//引入mysql模块
var mysql = require("mysql");
//创建并设置数据库连接
var conn = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "root",
    database: "itcast"
});
//打开连接
conn.connect();
//通过用户名查询数据
module.exports.queryDataByUserName = function (userName, callback) {
    var sql = "SELECT * FROM users WHERE userName = '" + userName + "'";
    conn.query(sql, (err, results, fields) => {
        if (err) {
            callback(err);
        }
        else {
            callback(null, results[0]);
        }
    })
}
//注册新用户数据插入
module.exports.insertNewUser = function (newUser, callback) {
    var sql = `INSERT INTO users (userName,password,nickName) VALUES ('${newUser.userName}','${newUser.password}','${newUser.userName}')`
    conn.query(sql, (err, results, fields) => {
        if (err) {
            callback(err);
        }
        else {
            callback(null, results.affectedRows);
        }
    })
}